package com.kgc.warehouse.system.controller;

import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.dto.RolePageDto;
import com.kgc.warehouse.system.entity.Role;
import com.kgc.warehouse.system.service.RolePermissionService;
import com.kgc.warehouse.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired(required = false)
    private RoleService roleService;

    @Autowired(required = false)
    private RolePermissionService rolePermissionService;

    @RequestMapping("all")
    public ResponseResult getAll()
    {
        List<Role> list = roleService.list(null);
        return new ResponseResult(list,Long.valueOf(list.size()), CodeMsg.SUCCESS);
    }

    //分页查询
    @RequestMapping("loadAllRole")
    @RequiresPermissions("role:view")
    public ResponseResult loadAllRole(RolePageDto rolePageDto)
    {

        return roleService.selectPage(rolePageDto);
    }

    //添加用户
    @RequestMapping("add")
    @RequiresPermissions("role:create")
    public ResponseResult add(Role role)
    {

        return roleService.add(role);
    }

    //添加用户
    @RequestMapping("delete")
    @RequiresPermissions("role:delete")
    public ResponseResult deleteById(String ids)
    {

        return roleService.deleteById(ids);
    }
    @RequestMapping("selectById")
    public ResponseResult selectById(Integer id)
    {

        Role byId = roleService.getById(id);
        return new ResponseResult(byId,null,CodeMsg.SUCCESS);
    }


    @RequestMapping("update")
    @RequiresPermissions("role:update")
    public ResponseResult update(Role role)
    {
        return roleService.updateRole(role);
    }

    @RequestMapping("selectPermissionByRoleId")
    public ResponseResult selectPermissionByRoleId(Integer id)
    {
        return roleService.selectPermissionByRoleId(id);
    }

    @RequestMapping("addRolePermission")
    public ResponseResult addRolePermission(Integer id,String permissionIds)
    {

        log.info(permissionIds);
        log.info(String.valueOf(id));
        return rolePermissionService.addRolePermission(id,permissionIds);
    }



}
